package com.zone.gateway.center.infrastructure.repository;

import cn.hutool.core.collection.CollectionUtil;
import com.zone.gateway.center.domain.manage.model.entity.GatewayServerDetailEntity;
import com.zone.gateway.center.domain.manage.model.entity.GatewayServerEntity;
import com.zone.gateway.center.domain.manage.model.vo.ApplicationInterfaceMethodVO;
import com.zone.gateway.center.domain.manage.model.vo.ApplicationInterfaceVO;
import com.zone.gateway.center.domain.manage.model.vo.ApplicationSystemVO;
import com.zone.gateway.center.domain.manage.repository.IGatewayConfigManageRepository;
import com.zone.gateway.center.infrastructure.dao.*;
import com.zone.gateway.center.infrastructure.po.*;
import com.zone.gateway.common.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2025/4/24
 */
@Repository
public class GatewayConfigManageRepository implements IGatewayConfigManageRepository {
    @Autowired
    private IGatewayServerDao gatewayServerDao;
    @Autowired
    private IGatewayServerDetailDao gatewayServerDetailDao;
    @Autowired
    private IGatewayDistributionDao gatewayDistributionDao;
    @Autowired
    private IApplicationSystemDao applicationSystemDao;
    @Autowired
    private IApplicationInterfaceDao applicationInterfaceDao;
    @Autowired
    private IApplicationInterfaceMethodDao applicationInterfaceMethodDao;

    @Override
    public List<GatewayServerEntity> queryGatewayServerList() {
        List<GatewayServer> gatewayServerList = gatewayServerDao.queryGatewayServerList();
        return gatewayServerList.stream()
                .map(gatewayServer -> {
                    GatewayServerEntity gatewayServerEntity = new GatewayServerEntity();
                    gatewayServerEntity.setId(gatewayServer.getId());
                    gatewayServerEntity.setGroupId(gatewayServer.getGroupId());
                    gatewayServerEntity.setGroupName(gatewayServer.getGroupName());
                    return gatewayServerEntity;
                }).collect(Collectors.toList());
    }

    @Override
    public GatewayServerDetailEntity queryGatewayServerDetail(String gatewayId, String gatewayAddress) {
        GatewayServerDetail gatewayServerDetail = gatewayServerDetailDao.queryGatewayServerDetail(gatewayId, gatewayAddress);
        if (null == gatewayServerDetail) {
            return null;
        }
        GatewayServerDetailEntity gatewayServerDetailEntity = new GatewayServerDetailEntity();
        gatewayServerDetailEntity.setGatewayId(gatewayServerDetail.getGatewayId());
        gatewayServerDetailEntity.setGatewayName(gatewayServerDetail.getGatewayName());
        gatewayServerDetailEntity.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
        gatewayServerDetailEntity.setStatus(gatewayServerDetail.getStatus());
        return gatewayServerDetailEntity;
    }

    @Override
    public void regisGatewayServerDetail(GatewayServerDetailEntity gatewayServerDetail) {
        GatewayServerDetail detail = new GatewayServerDetail();
        detail.setGroupId(gatewayServerDetail.getGroupId());
        detail.setGatewayId(gatewayServerDetail.getGatewayId());
        detail.setGatewayName(gatewayServerDetail.getGatewayName());
        detail.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
        detail.setStatus(gatewayServerDetail.getStatus());
        gatewayServerDetailDao.insert(detail);
    }

    @Override
    public void updateGatewayServerDetailStatus(String gatewayId, String gatewayAddress, String status) {
        gatewayServerDetailDao.updateStatus(gatewayId, gatewayAddress, status);
    }

    @Override
    public List<String> querySystemIdByGateway(String gatewayId) {
        return gatewayDistributionDao.querySystemIdByGateway(gatewayId);
    }

    @Override
    public List<ApplicationSystemVO> queryApplicaionSystemVoList(List<String> systemIds) {
        List<ApplicationSystemVO> resultList = new ArrayList<>();
        if (CollectionUtil.isEmpty(systemIds)) {
            return resultList;
        }
        List<ApplicationSystem> applicationSystems = applicationSystemDao.queryApplicationSystemList(systemIds);
        List<ApplicationInterface> applicationInterfaceList = applicationInterfaceDao.queryApplicationInterfaceList(systemIds);
        Map<String, List<ApplicationInterface>> applicationInterfaceMap = applicationInterfaceList.stream().collect(Collectors.groupingBy(ApplicationInterface::getSystemId));
        List<ApplicationInterfaceMethod> applicationInterfaceMethodList = applicationInterfaceMethodDao.queryApplicationInterfaceMethodList(systemIds);
        Map<String, List<ApplicationInterfaceMethod>> applicationInterfaceMethodMap = applicationInterfaceMethodList.stream().collect(Collectors.groupingBy(ApplicationInterfaceMethod::getInterfaceId));

        // 构造返回值
        return applicationSystems.stream()
                .map(applicationSystem -> {
                    // 系统
                    ApplicationSystemVO applicationSystemVO = new ApplicationSystemVO();
                    applicationSystemVO.setSystemId(applicationSystem.getSystemId());
                    applicationSystemVO.setSystemName(applicationSystem.getSystemName());
                    applicationSystemVO.setSystemType(applicationSystem.getSystemType());
                    applicationSystemVO.setSystemRegistry(applicationSystem.getSystemRegistry());
                    // 接口
                    List<ApplicationInterfaceVO> interfaceList = applicationInterfaceMap.getOrDefault(applicationSystem.getSystemId(), new ArrayList<>()).stream()
                            .map(applicationInterface -> {
                                ApplicationInterfaceVO applicationInterfaceVO = new ApplicationInterfaceVO();
                                applicationInterfaceVO.setSystemId(applicationInterface.getSystemId());
                                applicationInterfaceVO.setInterfaceId(applicationInterface.getInterfaceId());
                                applicationInterfaceVO.setInterfaceName(applicationInterface.getInterfaceName());
                                applicationInterfaceVO.setInterfaceVersion(applicationInterface.getInterfaceVersion());
                                // 方法
                                List<ApplicationInterfaceMethodVO> methodList = applicationInterfaceMethodMap.getOrDefault(applicationInterface.getInterfaceId(), new ArrayList<>()).stream()
                                        .map(applicationInterfaceMethod -> {
                                            ApplicationInterfaceMethodVO applicationInterfaceMethodVO = new ApplicationInterfaceMethodVO();
                                            applicationInterfaceMethodVO.setSystemId(applicationInterfaceMethod.getSystemId());
                                            applicationInterfaceMethodVO.setInterfaceId(applicationInterfaceMethod.getInterfaceId());
                                            applicationInterfaceMethodVO.setMethodId(applicationInterfaceMethod.getMethodId());
                                            applicationInterfaceMethodVO.setMethodName(applicationInterfaceMethod.getMethodName());
                                            applicationInterfaceMethodVO.setParameterType(applicationInterfaceMethod.getParameterType());
                                            applicationInterfaceMethodVO.setUri(applicationInterfaceMethod.getUri());
                                            applicationInterfaceMethodVO.setHttpCommandType(applicationInterfaceMethod.getHttpCommandType());
                                            applicationInterfaceMethodVO.setAuth(applicationInterfaceMethod.getAuth());
                                            return applicationInterfaceMethodVO;
                                        }).collect(Collectors.toList());
                                applicationInterfaceVO.setMethodList(methodList);
                                return applicationInterfaceVO;
                            }).collect(Collectors.toList());
                    applicationSystemVO.setInterfaceList(interfaceList);
                    return applicationSystemVO;
                }).collect(Collectors.toList());
    }

    @Override
    public List<String> queryGatewayIds(String systemId) {
        return gatewayDistributionDao.queryGatewayIds(systemId);
    }
}
